package com.mxx.common.generator.db;

import com.mxx.common.core.UniquelyList;
import com.mxx.common.generator.EntityInfo;
import com.mxx.common.generator.FileGeneratorManager;
import com.mxx.common.generator.GlobalNameConfig;
import com.mxx.common.generator.convert.NameConverter;
import com.mxx.common.generator.convert.TableToEntityInfo;
import com.mxx.common.generator.convert.TypeConvertHandler;
import lombok.SneakyThrows;

import java.sql.Connection;
import java.util.Set;

public class DBFileGeneratorManager implements FileGeneratorManager {

    private String url;

    private String username;

    private String password;

    private TypeConvertHandler typeConvertHandler;

    private GlobalNameConfig globalNameConfig;

    private TableToEntityInfo tableToEntityInfo;

    private NameConverter nameConverter;

    public DBFileGeneratorManager(String url, String username, String password) {
        this.url = url;
        this.username = username;
        this.password = password;
    }

    @Override
    @SneakyThrows
    public UniquelyList<String, EntityInfo> generator() {

        MysqlDatabaseConnection databaseConnection = new MysqlDatabaseConnection();

        try (Connection conn = databaseConnection.connection(url, username, password)) {
            TableReader reader = new MysqlTableReader(conn);
            UniquelyList<String, Table> tableList = reader.readAll();

            UniquelyList<String, EntityInfo> entityInfoList = new UniquelyList<>(EntityInfo::getEntityName);

            for (Table table : tableList) {
                entityInfoList.add(tableToEntityInfo.toEntity(table));
            }
            return entityInfoList;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    @Override
    @SneakyThrows
    public UniquelyList<String, EntityInfo> generator(Set<String> nameSet) {

        MysqlDatabaseConnection databaseConnection = new MysqlDatabaseConnection();

        try (Connection conn = databaseConnection.connection(url, username, password)) {
            UniquelyList<String, EntityInfo> entityInfoList = new UniquelyList<>(EntityInfo::getEntityName);
            TableReader reader = new MysqlTableReader(conn);

            for (String name : nameSet) {
                Table table = reader.reader(name);
                entityInfoList.add(tableToEntityInfo.toEntity(table));
            }

            return entityInfoList;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    public void setTableToEntityInfo(TableToEntityInfo tableToEntityInfo) {
        this.tableToEntityInfo = tableToEntityInfo;
    }

    public void setNameConverter(NameConverter nameConverter) {
        this.nameConverter = nameConverter;
    }

    public void setGlobalNameConfig(GlobalNameConfig globalNameConfig) {
        this.globalNameConfig = globalNameConfig;
    }

    public void setTypeConvertHandler(TypeConvertHandler typeConvertHandler) {
        this.typeConvertHandler = typeConvertHandler;
    }

    public GlobalNameConfig getGlobalNameConfig() {
        return globalNameConfig;
    }

    public NameConverter getNameConverter() {
        return nameConverter;
    }

    public TableToEntityInfo getTableToEntityInfo() {
        return tableToEntityInfo;
    }

    public TypeConvertHandler getTypeConvertHandler() {
        return typeConvertHandler;
    }

    public String getUrl() {
        return url;
    }

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }

}
